<template>
    <div class="monitor-page">
        <el-row>
            <el-col :span="5">
                <div class="tree">
                    <el-input placeholder="输入关键字进行过滤" v-model="filterText">
                    </el-input>
                    <el-tree @node-click="handlerClick" class="filter-tree" :data="EngData" :props="defaultProps"
                        :filter-node-method="filterNode" ref="tree">
                    </el-tree>
                </div>
            </el-col>
            <el-col :span="19">
                <div v-if="params.device_id" class="table">
                    <el-form :inline="true" :model="form">
                        <el-form-item>
                            <el-date-picker value-format="timestamp" v-model="form.time" type="datetimerange"
                                :picker-options="pickerOptions" range-separator="至" start-placeholder="开始日期"
                                end-placeholder="结束日期" align="right">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="toSearch">搜索</el-button>
                        </el-form-item>
                    </el-form>
                    <!-- 表格  -->
                    <el-table :data="monitorData" style="width: 100%" :row-class-name="tableRowClassName">
                        <el-table-column prop="insert_time" label="日期" width="180" align="center">
                            <template v-slot="scope">
                                <div>
                                    {{ fmtDate(scope.row.insert_time) }}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="tsp" label="tsp" width="180">
                        </el-table-column>
                        <el-table-column prop="pm10" label="pm10">
                        </el-table-column>
                        <el-table-column prop="pm25" label="pm2.5">
                        </el-table-column>
                        <el-table-column prop="temperature" label="温度">
                        </el-table-column>
                        <el-table-column prop="humidity" label="湿度">
                        </el-table-column>
                        <el-table-column prop="wind_direction" label="风向">
                        </el-table-column>
                        <el-table-column prop="wind_speed" label="风速">
                        </el-table-column>
                    </el-table>

                    <!-- 分页组件 -->
                    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        :current-page="params.page" :page-sizes="[10, 20, 50, 100]" :page-size="params.pageSize"
                        layout="total, sizes, prev, pager, next, jumper" :total="total">
                    </el-pagination>
                </div>
                <div v-else style="line-height: 50px;color: red;font-size: 20px;text-align: center;">请点击设备查看数据</div>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import { get } from '@/utils/request'
export default {
    data() {
        return {
            filterText: '',
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            monitorData: [],
            form: { time: [] },
            EngData: [],
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
            params: {
                page: 1,
                pageSize: 10,
                device_id: 0,
                engineer_id: 0
            },
            total: 0 // 总数据条数
        }
    },
    watch: {
        filterText(val) {
            this.$refs.tree.filter(val);
        }
    },
    methods: {
        // 日期格式化方法
        fmtDate(time) {
            if (!time) return '';
            const date = new Date(time);
            return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
        },
        handlerClick(val, node) {
            if (!val.children) {
                this.params.device_id = val.id;
                this.params.engineer_id = val.engineer_id;
                this.params.page = 1; // 切换设备时重置页码
                this.getMonitor();
            }
        },
        toSearch() {
            this.params.page = 1;
            this.getMonitor();
        },
        // 处理每页条数变化
        handleSizeChange(val) {
            this.params.pageSize = val;
            this.params.page = 1;
            this.getMonitor();
        },
        // 处理当前页码变化
        handleCurrentChange(val) {
            this.params.page = val;
            this.getMonitor();
        },
        async getMonitor() {
            let temp = { ...this.params };
            if (this.form.time) {
                temp.begin_time = this.form.time[0];
                temp.end_time = this.form.time[1];
            }
            let res = await get('/monitor/pageQuery', temp);
            this.monitorData = res.data.list;
            this.total = res.data.total; // 获取总条数
        },
        async getEngDevData() {
            let res = await get('/engineer/findEngineerDeviceTree');
            this.EngData = res.data;
        },
        filterNode(value, data) {
            if (!value) return true;
            return data.name.indexOf(value) !== -1;
        },
        tableRowClassName({ row, rowIndex }) {
            if (rowIndex % 2 == 0 || rowIndex == 0) {
                return 'warning-row';
            } else {
                return 'success-row';
            }
            return '';
        }
    },
    created() {
        this.getEngDevData();
        this.getMonitor();
    }
}
</script>
<style lang="scss" scoped>
// 新增页面外层容器，添加粉紫色背景
.monitor-page {
    min-height: 100vh;
    background-color: rgba(236, 192, 255, 0.3);
    padding: 15px; // 可选：添加内边距，避免内容贴边
}

.tree {
    height: calc(100vh - 90px); // 调整高度，适配外层容器内边距
    padding-right: 10px;
    border-right: 1px solid #ececec;
    margin-right: 10px;
    // 为树形区域添加白色背景，提升内容可读性
    background-color: #fff;
    padding: 10px;
    border-radius: 8px;
}

.table {
    // 为表格区域添加白色背景，与树形区域风格统一
    background-color: #fff;
    padding: 15px;
    border-radius: 8px;
}

::v-deep .el-table .warning-row {
    background: oldlace;
}

::v-deep .el-table .success-row {
    background: #f0f9eb;
}

// 为“请点击设备查看数据”文本区域添加白色背景
::v-deep .el-col:nth-child(2)>div:not(.table) {
    background-color: #fff;
    min-height: 200px;
    border-radius: 8px;
    margin-top: 10px;
}
</style>